package br.com.dani3lviana.loja.infra;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;
import br.com.caelum.vraptor.ioc.ComponentFactory;

@Component
@ApplicationScoped
public class CriadorDeSessionFactory implements ComponentFactory<SessionFactory>{
	private SessionFactory factory;
	
	@Override
	public SessionFactory getInstance() {
		return factory;
	}
	
	@PostConstruct
	public void criarFabrica(){
		this.factory = new Configuration().configure().buildSessionFactory();
	}
	
	@PreDestroy
	public void destroiFabrica(){
		this.factory.close();
	}

}
